if SERVER then
	function LoadUnbanMenu(pl, cmd, args)
		if !pl:KAdmin() && !pl:KModerator() then return end
	
		pl:SendLua("table.Empty(kontrol_unban_tbl)")
	
		if SQL_ACTIVE then
			local query = "SELECT * FROM kontrol"
			tmysql.query(query, function(result, status, error)
				if #result > 0 then
					for k,v in pairs(result) do
						if tonumber(v["unbanTime"]) - os.time() <= 0 && tonumber(v["length"]) != 0 then
							game.ConsoleCommand("removeid " .. v["id"] .. "\n")
							query = "DELETE FROM kontrol WHERE id='" .. v["id"] .. "'"
							tmysql.query(query)
						else
							pl:SendLua("kontrol_unban_tbl[" .. k .. "] = '" .. tostring(v["name"] .. ";" .. v["id"] .. ";" .. v["reason"]) .. ";" .. math.floor((tonumber(v["unbanTime"]) - os.time()) / 60) .. ";" .. v["admin"] .. "'")
						end
					end
				end
				
				if ValidEntity(pl) then
					pl:SendLua("UnbanMenu()")
				end
			end, 1)
		else
			local query = "SELECT * FROM kontrol"
			local result = sql.Query(query)
			if result then
				for k,v in pairs(result) do
					if tonumber(v["unbanTime"]) - os.time() <= 0 && tonumber(v["length"]) != 0 then
						game.ConsoleCommand("removeid " .. v["id"] .. "\n")
						query = "DELETE FROM kontrol WHERE id='" .. v["id"] .. "'"
						result = sql.Query(query)
					else
						pl:SendLua("kontrol_unban_tbl[" .. k .. "] = '" .. tostring(v["name"] .. ";" .. v["id"] .. ";" .. v["reason"]) .. ";" .. math.floor((tonumber(v["unbanTime"]) - os.time()) / 60) .. ";" .. v["admin"] .. "'")
					end
				end
			end
			
			timer.Simple(0.1, function()
				if ValidEntity(pl) then
					pl:SendLua("UnbanMenu()")
				end
			end)
		end
	end
	concommand.Add("sv_kontrol_unbanmenu", LoadUnbanMenu)
	
	function EditBan(pl, cmd, args)
		if !pl:KAdmin() then return end
		
		local admin = pl
		local length = tonumber(args[2])
		local steamid = args[1]
		
		if SQL_ACTIVE then
			local query = "SELECT * FROM kontrol WHERE id='" .. steamid .. "'"
			tmysql.query(query, function(result, status, error)
				if #result > 0 then
					query = "UPDATE kontrol SET unbanTime=" .. length + os.time() .. ", length=" .. length / 60 .. " WHERE id='" .. steamid .. "'"
					tmysql.query(query)
					
					admin:SendLua("UNBAN_MENU:Close()")
					LoadUnbanMenu(admin)
				end
			end, 1)
		else
			local query = "SELECT * FROM kontrol WHERE id='" .. steamid .. "'"
			local result = sql.Query(query)
			if result then
				query = "UPDATE kontrol SET unbanTime=" .. length + os.time() .. ", length=" .. length / 60 .. " WHERE id='" .. steamid .. "'"
				result = sql.Query(query)
				
				admin:SendLua("UNBAN_MENU:Close()")
				LoadUnbanMenu(admin)
			end
		end
	end
	concommand.Add("sv_kontrol_editban", EditBan)
	
	function AddBan(pl, cmd, args)
		if !pl:KAdmin() then return end
		
		local admin = pl
		local reason = args[3]
		local length = tonumber(args[2])
		local steamid = args[1]
		if SQL_ACTIVE then
			local query = "INSERT INTO kontrol (`id`, `name`, `reason`, `length`, `admin`, `date`, `unbanTime`) VALUES ('" .. steamid .. "', 'Unknown', '" .. tmysql.escape(reason) .. "', " .. length / 60 .. ", '" .. tmysql.escape(admin:Name()) .. "', '" .. os.date() .. "', " .. os.time() + length .. ")"
			tmysql.query(query)
			game.ConsoleCommand("banid " .. length / 60 .. " " .. steamid .. "\n")
		else
			local query = "INSERT INTO kontrol (`id`, `name`, `reason`, `length`, `admin`, `date`, `unbanTime`) VALUES ('" .. steamid .. "', 'Unknown', '" .. reason .. "', " .. length / 60 .. ", '" .. admin:Name() .. "', '" .. os.date() .. "', " .. os.time() + length .. ")"
			local result = sql.Query(query)
			game.ConsoleCommand("banid " .. length / 60 .. " " .. steamid .. "\n")
		end
		
		admin:SendLua("UNBAN_MENU:Close()")
		LoadUnbanMenu(admin)
	end
	concommand.Add("sv_kontrol_addban", AddBan)
	
	function Unban(pl, cmd, args)
		if !pl:KAdmin() then return end
		
		local admin = pl
		local length = tonumber(args[2])
		local steamid = args[1]
		
		if SQL_ACTIVE then
			local query = "SELECT * FROM kontrol WHERE id='" .. steamid .. "'"
			tmysql.query(query, function(result, status, error)
				if result then
					game.ConsoleCommand("removeid " .. steamid .. "\n")
					query = "DELETE FROM kontrol WHERE id='" .. steamid .. "'"
					tmysql.query(query)
					
					admin:SendLua("UNBAN_MENU:Close()")
					LoadUnbanMenu(admin)
				end
			end)
		else
			local query = "SELECT * FROM kontrol WHERE id='" .. steamid .. "'"
			local result = sql.Query(query)
			if result then
				game.ConsoleCommand("removeid " .. steamid .. "\n")
				query = "DELETE FROM kontrol WHERE id='" .. steamid .. "'"
				result = sql.Query(query)
				
				admin:SendLua("UNBAN_MENU:Close()")
				LoadUnbanMenu(admin)
			end
		end
	end
	concommand.Add("sv_kontrol_unban", Unban)
end

if CLIENT then
	kontrol_unban_tbl = {}
	
	function EditBanParamBox(name, steamid, length)
		local menu = vgui.Create("KFrame2")
		menu:SetSize(300, 102)
		menu:SetPos(ScrW() / 2 - 150, ScrH() / 2 - 51)
		menu:SetTitle("Edit Ban")
		menu:SetDraggable(true)
		menu:ShowCloseButton(true)
		menu:SetSizable(false)
		menu.PaintOver = function()
			draw.DrawText("Ban Length in Minutes", "default", 10, 32, Color(70, 70, 70, 255), TEXT_ALIGN_LEFT)
		end
		
		local choice = 1
		local lengthBox = vgui.Create("DMultiChoice", menu)
		lengthBox:SetPos(10, 47)
		lengthBox:SetSize(280, 20)
		lengthBox:SetText(length)
		lengthBox.OnSelect = function(panel, i, value)
			choice = lengthBox:GetOptionText(i)
		end
		lengthBox:AddChoice("5 Minutes")
		lengthBox:AddChoice("30 Minutes")
		lengthBox:AddChoice("1 Hour")
		lengthBox:AddChoice("3 Hours")
		lengthBox:AddChoice("12 Hours")
		lengthBox:AddChoice("1 Day")
		lengthBox:AddChoice("2 Days")
		lengthBox:AddChoice("3 Days")
		lengthBox:AddChoice("5 Days")
		lengthBox:AddChoice("1 Week")
		lengthBox:AddChoice("2 Weeks")
		lengthBox:AddChoice("1 Month")
		lengthBox:AddChoice("Permanent")
		
		local accept = vgui.Create("KButton", menu)
		accept:SetSize(100, 20)
		accept:SetPos(100, 72)
		accept:SetText("Accept")
		accept.DoClick = function()
			length = choice
		
			if length == "Permanent" then
				length = 0
			elseif length == "1 Month" then
				length = 40320
			elseif length == "2 Weeks" then
				length = 20160
			elseif length == "1 Week" then
				length = 10080
			elseif length == "5 Days" then
				length = 7200
			elseif length == "3 Days" then
				length = 4320
			elseif length == "2 Days" then
				length = 2880
			elseif length == "1 Day" then
				length = 1440
			elseif length == "12 Hours" then
				length = 720
			elseif length == "3 Hours" then
				length = 180
			elseif length == "1 Hour" then
				length = 60
			elseif length == "30 Minutes" then
				length = 30
			elseif length == "5 Minutes" then
				length = 5
			end
			
			RunConsoleCommand("sv_kontrol_editban", steamid, length * 60)
			menu:Close()
		end
		
		menu:MakePopup()
	end
	
	function AddBanParamBox()
		local menu = vgui.Create("KFrame2")
		menu:SetSize(300, 177)
		menu:SetPos(ScrW() / 2 - 150, ScrH() / 2 - 88)
		menu:SetTitle("Add Ban")
		menu:SetDraggable(true)
		menu:ShowCloseButton(true)
		menu:SetSizable(false)
		menu.PaintOver = function()
			draw.DrawText("Offender's Steam ID", "default", 10, 32, Color(70, 70, 70, 255), TEXT_ALIGN_LEFT)
		end
		
		local steamIDBox = vgui.Create("DTextEntry", menu)
		steamIDBox:SetPos(10, 47)
		steamIDBox:SetSize(280, 20)
		
		local reasonBox = vgui.Create("DTextEntry", menu)
		reasonBox:SetPos(10, 77)
		reasonBox:SetSize(280, 20)
		reasonBox:SetText("Reason for Ban")
		
		local length = "5 Minutes"
		local lengthBox = vgui.Create("DMultiChoice", menu)
		lengthBox:SetPos(10, 107)
		lengthBox:SetSize(280, 20)
		lengthBox:SetText("5 Minutes")
		lengthBox.OnSelect = function(panel, i, value)
			length = value
		end
		lengthBox:AddChoice("5 Minutes")
		lengthBox:AddChoice("30 Minutes")
		lengthBox:AddChoice("1 Hour")
		lengthBox:AddChoice("3 Hours")
		lengthBox:AddChoice("12 Hours")
		lengthBox:AddChoice("1 Day")
		lengthBox:AddChoice("2 Days")
		lengthBox:AddChoice("3 Days")
		lengthBox:AddChoice("5 Days")
		lengthBox:AddChoice("1 Week")
		lengthBox:AddChoice("2 Weeks")
		lengthBox:AddChoice("1 Month")
		lengthBox:AddChoice("Permanent")
		
		local accept = vgui.Create("KButton", menu)
		accept:SetSize(100, 20)
		accept:SetPos(100, 147)
		accept:SetText("Accept")
		accept.DoClick = function()
			if length == "Permanent" then
				length = 0
			elseif length == "1 Month" then
				length = 40320
			elseif length == "2 Weeks" then
				length = 20160
			elseif length == "1 Week" then
				length = 10080
			elseif length == "5 Days" then
				length = 7200
			elseif length == "3 Days" then
				length = 4320
			elseif length == "2 Days" then
				length = 2880
			elseif length == "1 Day" then
				length = 1440
			elseif length == "12 Hours" then
				length = 720
			elseif length == "3 Hours" then
				length = 180
			elseif length == "1 Hour" then
				length = 60
			elseif length == "30 Minutes" then
				length = 30
			elseif length == "5 Minutes" then
				length = 5
			end
			
			local steamid = steamIDBox:GetValue()
			local reason = reasonBox:GetValue()
			if reason == "Reason for Ban" then reason = "No reason specified." end
			
			RunConsoleCommand("sv_kontrol_addban", steamid, length * 60, reason)
			menu:Close()
		end
		
		menu:MakePopup()
	end
	
	function UnbanMenu()
		local selection
	
		UNBAN_MENU = vgui.Create("KFrame2")
		UNBAN_MENU:SetSize(600, 400)
		UNBAN_MENU:SetPos(ScrW() / 2 - 300, ScrH() / 2 - 200)
		UNBAN_MENU:SetTitle("Ban List")
		UNBAN_MENU:SetDraggable(true)
		UNBAN_MENU:ShowCloseButton(true)
		UNBAN_MENU:SetSizable(false)
		
		banlist = vgui.Create("DListView", UNBAN_MENU)
		banlist:SetPos(10, 32)
		banlist:SetSize(580, 328)
		banlist:SetMultiSelect(false)
		banlist:AddColumn("Name")
		banlist:AddColumn("Steam ID")
		banlist:AddColumn("Reason")
		banlist:AddColumn("Remaining")
		banlist:AddColumn("Admin")
		banlist:SortByColumn(4)
		banlist.OnRowSelected = function(parent, line)
			selection = banlist:GetLine(line)
		end
		for k,v in pairs(kontrol_unban_tbl) do
			local bans = string.Explode(";", v)
			banlist:AddLine(bans[1], bans[2], bans[3], bans[4] .. " minutes", bans[5])
		end
		
		local unbanBut = vgui.Create("KButton", UNBAN_MENU)
		unbanBut:SetPos(490, 368)
		unbanBut:SetSize(100, 20)
		unbanBut:SetText("Unban")
		unbanBut.DoClick = function()
			if selection then
				RunConsoleCommand("sv_kontrol_unban", selection:GetValue(2))
			end
		end
		
		local lengthBut = vgui.Create("KButton", UNBAN_MENU)
		lengthBut:SetPos(380, 368)
		lengthBut:SetSize(100, 20)
		lengthBut:SetText("Edit Length..")
		lengthBut.DoClick = function()
			if selection then
				EditBanParamBox(selection:GetValue(1), selection:GetValue(2), selection:GetValue(4))
			end
		end
		
		local addBan = vgui.Create("KButton", UNBAN_MENU)
		addBan:SetPos(10, 368)
		addBan:SetSize(100, 20)
		addBan:SetText("Add Ban..")
		addBan.DoClick = function()
			AddBanParamBox()
		end
		
		UNBAN_MENU:MakePopup()
	end
end